package com.xiaoy.core.database.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;


import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Results;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.xiaoy.core.database.entity.Column;
import com.xiaoy.core.database.entity.DataTable;
import com.xiaoy.core.database.service.DatabaseService;
import com.xiaoy.util.json.JSONUtils;
import com.xiaoy.web.BasicAction;
@Namespace("/database")
@Results( {
})
public class DatabaseAction extends BasicAction{

	@Resource(name="databaseService")
	private DatabaseService service ;
	
	public String list () {
		
//		request.setAttribute("dataList", service.getDataTables());
		
		request.setAttribute("listData", JSONUtils.toJson(service.getDataTables()));
		
		
		return "list";
	}
	
	public String expAllTableInfo () {
		
		List<DataTable> list = new ArrayList<DataTable>();
		
//		service.getDataTables();
		
		request.setAttribute("pageList", service.getDataTables());
		
		return "expalltableinfo";
	}
	
	public String expTableFieldInfo (){
		
		String tablename = getParameter("tablename");
		request.setAttribute("pageList", service.getDBColumnsByTablename(tablename));
		return "exptablefieldinfo";
	}
	
	public void tableInfo (){
		try{
			String tablename = getParameter("tablename");
			List<Column> list = service.getDBColumnsByTablename(tablename);
			
//			for (Column c : list) {
//				System.out.println(c.getName()+"-"+c.getType()+"-"+c.getDefaultValue()+"-"+c.getLength()+"-"+c.getInfo());
//			}
			
			String info = JSONUtils.toJson(list);
			System.out.println(info);
			renderJson(info);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public void getTablesByUser () {
		
		
		Struts2Utils.renderJson("");
	}
	
	
	
	
}
